<!--
 * Copyright (c) 2018-2025, duneng All rights reserved.
 *
 * Author: wq (meteor85@163.com) 
 * 2018年10月9日 目前仅为静态数据
-->

<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit" v-if="roleManager_btn_add">添加
      </el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" height="380" element-loading-text="正在努力加载中..." border fit highlight-current-row style="width: 99%">

      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="企业名称" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.companyName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="企业概述" width="380">
        <template slot-scope="scope">
          <span>{{scope.row.companyDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="企业类别" width="120px">
        <template slot-scope="scope">
          <!-- <span>{{scope.row.companyType}}</span> -->
          <span>{{compType[scope.row.companyType]}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="所属园区" width="180">
        <template slot-scope="scope">
          <!-- <span>{{scope.row.industryId }}</span> -->
          <el-select v-model="scope.row.industryId" placeholder="所属园区" disabled="">
            <el-option v-for="item in industryListTo" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
          </el-select>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="详细地址" width="280">
        <template slot-scope="scope">
          <span>{{scope.row.companyAddress}}</span>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="success" v-if="roleManager_btn_edit" @click="viewDetail(scope.row)">详细
          </el-button>
          <el-button size="mini" type="success" v-if="roleManager_btn_edit" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button size="mini" type="danger" v-if="roleManager_btn_del" @click="handleDelete(scope.row)">删除
          </el-button>
          <!-- <el-button size="mini" type="info" plain @click="handlePermission(scope.row)" v-if="roleManager_btn_perm">权限
          </el-button> -->
        </template>
      </el-table-column>

    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="企业名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="企业名称"></el-input>
        </el-form-item>
        <el-form-item label="企业概述" prop="companyDesc" >
          <el-input type="textarea" v-model="form.companyDesc" placeholder="企业概述" style="height:100px"></el-input>
        </el-form-item>
        <el-form-item label="企业类别" prop="companyType">
          <!-- <el-input v-model="form.companyType" placeholder="工业园类别"></el-input> -->
            <el-radio v-model="form.companyType" label="0" border>虚拟</el-radio>
            <el-radio v-model="form.companyType" label="1" border>真实</el-radio>
        </el-form-item>
        <el-form-item label="规模系数" v-if="form.companyType=='0'" prop="form.companyCoefficient">
                <el-select v-model="companyCoefficient" placeholder="规模系数">
                  <el-option
                    v-for="item in optionCoefficient"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
            </el-form-item>
        <el-form-item label="模拟对象"  v-if="form.companyType=='0'" prop="form.companySimulate">
          <el-select v-model="form.companySimulate" placeholder="模拟企业名称">
              <el-option v-for="item in companySimulate" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="所属园区" prop="companyIndustry">
          <!-- <el-input v-model="form.industryId" placeholder="所属园区"></el-input> -->
          <el-select v-model="form.industryId" placeholder="所属园区" @change="setIndustryInfo">
            <el-option v-for="item in industryListTo" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="企业类别" prop="companyType">
            <el-input  type="textarea"  v-model="form.companyType" placeholder="这个地方待提供分类数据后，做成下拉框形式。"></el-input>
        </el-form-item>
    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>

    <!--<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogDeptVisible">
      <el-tree class="filter-tree" :data="treeDeptData" :default-checked-keys="checkedKeys" check-strictly node-key="id" highlight-current ref="deptTree" @node-click="getNodeData" :props="defaultProps" :filter-node-method="filterNode" default-expand-all>
      </el-tree>
    </el-dialog>-->

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogPermissionVisible">
      <el-tree class="filter-tree" :data="treeData" :default-checked-keys="checkedKeys" check-strictly node-key="id" highlight-current :props="defaultProps" show-checkbox ref="menuTree" :filter-node-method="filterNode" default-expand-all>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updatePermession(provinceId, provinceType)">更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  getCompanyByUserPermission
} from '@/api/bus/sensor/company';
import{
  getIndustryByUserPermission
} from '@/api/bus/sensor/industry';
import { fetchTree } from '@/api/menu';
import { mapGetters } from 'vuex';
import waves from '@/directive/waves/index.js'; // 水波纹指令
import {regionData } from 'element-china-area-data';//引入省市区三级联动数据
export default {
  name: 'table_role',
  directives: {
    waves
  },
  data() {
    return {
      options:regionData,  //为了省市区下拉框处理数据
      provinceCity: [],//为了省市区下拉框处理数据
      treeData: [],
      treeDeptData: [],
      checkedKeys: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      compType:['虚拟','真实'],
      form: {
        companyName: undefined,
        companyDesc: undefined,
        industryId:undefined,
        companySimulate:undefined,
        cityId:undefined,
        provinceId:undefined,
        factor:1
      },
      companyCoefficient:'1',
      companySimulate:[],
      companyList:[],
      industryList:[],
      industryListTo:[],
      selectedInudstry:[],
      optionCoefficient:[
        {value: '0.5',  label: '50%'},
        {value: '0.55',  label: '55%'},
        {value: '0.60',  label: '60%'},
        {value: '0.65',  label: '65%'},
        {value: '0.70',  label: '70%'},
        {value: '0.75',  label: '75%'},
        {value: '0.80',  label: '80%'},
        {value: '0.85',  label: '85%'},
        {value: '0.90',  label: '90%'},
        {value: '0.95',  label: '95%'},
        {value: '1',  label: '100%'},
        {value: '1.05',  label: '105%'},
        {value: '1.1',  label: '110%'},
        {value: '1.15',  label: '115%'},
        {value: '1.2',  label: '120%'},
        {value: '1.25',  label: '125%'},
        {value: '1.3',  label: '130%'},
        {value: '1.35',  label: '135%'},
        {value: '1.4',  label: '140%'},
        {value: '1.45',  label: '145%'},
        {value: '1.5',  label: '150%'},
        ],
      
      // id: undefined,
      // roleCode: undefined,
      rules: {
        companyName: [

          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        companyDesc: [
          {
            min: 3,
            max: 200,
            message: '长度在 3 到 200 个字符',
            trigger: 'blur'
          }
        ],
        companyIndustry: [
          {
            min: 1,
            max: 20,
            message: '长度在 1 到 20 个字符',
            trigger: 'blur'
          }
        ]
      },
      statusOptions: ['0', '1'],
      rolesOptions: undefined,
      dialogFormVisible: false,
      dialogDeptVisible: false,
      dialogPermissionVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建',
        //permission: '分配权限'
      },
      tableKey: 0,
      tableKey: 0,
      roleManager_btn_add: false,
      roleManager_btn_edit: false,
      roleManager_btn_del: false,
      roleManager_btn_perm: false
    }
  },
  created() {
    this.getList()
    this.roleManager_btn_add = this.permissions['sys_province_add']
    this.roleManager_btn_edit = this.permissions['sys_province_edit']
    this.roleManager_btn_del = this.permissions['sys_province_del']

  },
  computed: {
    ...mapGetters(['elements', 'permissions'])
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      });
      //创建界面弹出的时候，将用户权限范围内的企业查询出来
      getCompanyByUserPermission().then(response=>{
        // console.log('查询用户权限范围内的企业列表')
        this.companyList = response.data;
        this.companySimulate=[];
        for (let index = 0; index < response.data.length; index++) {
          var vt = response.data[index];
          this.companySimulate.push({
            value:vt.id,
            label:vt.companyName
          })
        }
      })

      //创建界面弹出时，将用户权限范围内的园区查询出来
      getIndustryByUserPermission().then(response=>{
        this.industryList = response.data;
        this.industryListTo=[];
        for (let index = 0; index < response.data.length; index++) {
          var vt = response.data[index];
          this.industryListTo.push({
            value:vt.id,
            label:vt.industryName
          })
        }
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {

      this.resetTemp()
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
      
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        this.form = response.data
        // this.form.deptName = row.deptName
        // this.form.roleDeptId = row.roleDeptId
        this.dialogFormVisible = true
        this.dialogStatus = 'update';
      })
    },
    viewDetail(row){
        //查看企业详细信息图
        this.$router.push({
          path:'/bus/company/detail',
          components:'views/bus/company/detail',
          query:{
            'companyId':row.id,
          }
        });
    },
    
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getNodeData(data) {
      this.dialogDeptVisible = false
      this.form.roleDeptId = data.id
      this.form.deptName = data.name
      // console.log(data)
    },
    handleDelete(row) {
      delObj(row.id).then(response => {
        this.dialogFormVisible = false
        this.getList()
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    create(formName) {
      this.form.cityId = this.selectedInudstry.cityId;
      this.form.provinceId = this.selectedInudstry.provinceId;
      this.form.industryId = this.selectedInudstry.id;
      this.form.factor = this.companyCoefficient;
      console.log(this.form);
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          putObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
   
    resetTemp() {
      this.form = {
        id: undefined,
        companyName:undefined,
        companyDesc:undefined,
        companyIndustry:undefined,
        industryId:undefined,
        companyAddress:undefined,
        provinceCity:undefined,
        companyCoefficient:'1',
        cityId:undefined,
        provinceId:undefined
      }
    },
    setIndustryInfo(value){
      // selectedInudstry
      for (let index = 0; index < this.industryList.length; index++) {
        var vt = this.industryList[index];
        
        if(vt.id == value){
          this.selectedInudstry = vt;
          break;
        }
      }
    },
  }
}
</script>
